package cvi.com.reader;

import android.app.Application;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.iflytek.cloud.SpeechUtility;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.orhanobut.hawk.Hawk;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import cvi.com.reader.bean.constant;

/**
 * Created by WP on 2020/7/17.
 */

public class ReaderApplication extends Application {

    private static ReaderApplication mReaderApplication = null;
    private InetAddress address;
    private Integer port = 12345;
    private String ipStr;
    private boolean isFirst = true;
    Handler mHander = new Handler() {

        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
                case 1:
                    String str = (String) msg.obj;
                    ipStr = str.substring(4, str.length() - 4);
             /*       if (isFirst) {
                        Log.d("ewerrr", ipStr);
                        Hawk.put("ip", ipStr.trim());
                        isFirst = false;
                    } else {
                        if (Hawk.contains("ip")) {
                            String ip = Hawk.get("ip");
                            Log.d("ewerrr222", ipStr);
                            if (!ipStr.equals(ip)) {
                                Log.d("ewerrr333", ipStr);
                                Hawk.put("ip", ipStr.trim());
                                setChangeIp();
                            }
                        }

                    }*/
                    break;

            }


        }
    };

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
  /*      SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.locationtextcolor, android.R.color.white);//全局设置主题颜色
                return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });*/
    /*    //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });*/
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mReaderApplication = this;
        Hawk.init(this).build();
        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(this));
        SpeechUtility.createUtility(ReaderApplication.this, "appid=" + "5fbb2107");
      /*  //是否是第一安装
        SharedPreferences sharedPreferences = getSharedPreferences("is_first_in_data", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("isFirstIn", true);
        editor.commit();*/
        //  BaseExceptionHandler.getInstance().setContext(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] message = new byte[1024];
                try {
                    // 建立Socket连接
                    try {
                        address = InetAddress.getByName("255.255.255.255");
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    DatagramSocket datagramSocket = new DatagramSocket(port);
                    datagramSocket.setBroadcast(true);
                    DatagramPacket datagramPacket = new DatagramPacket(message,
                            message.length);

                    try {
                        while (true) {
                            int p = datagramPacket.getPort();
                            // 准备接收数据
                            datagramSocket.receive(datagramPacket);
                            String strMsg = new String(datagramPacket.getData(), datagramPacket.getOffset(), datagramPacket.getLength(), "UTF-8");
                            Message msg = new Message();
                       /*     msg.what = 1;
                            if (strMsg.length() > 9) {
                                if (".Cvi".equals(strMsg.substring(0, 4)) && ("End.".equals(strMsg.substring(strMsg.length() - 4, strMsg.length())))) {
                                    msg.obj = strMsg;
                                    mHander.sendMessage(msg);
                                }
                            }*/
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (SocketException e) {
                    e.printStackTrace();
                }


            }
        }).start();
    }

    private void setChangeIp() {
        Intent intent = new Intent(constant.CHANGE_IP);
        sendBroadcast(intent);      //发送广播

    }

    public static ReaderApplication getInstance() {
        return mReaderApplication;
    }

  /*  public boolean isLogin() {
        String userId = SharedPreferencesUtil.getString(this, AppModel.USERID, "");
        if (TextUtils.isEmpty(userId)) {
            return false;
        }
        return true;
    }*/
}
